/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * LumenWidgetDetector.h
 * Runs the Lumen discovery routine.
 * Copyright (C) 2011 Simon Newton
 */

#ifndef PLUGINS_USBPRO_LUMENWIDGETDETECTOR_H_
#define PLUGINS_USBPRO_LUMENWIDGETDETECTOR_H_

#include <ola/Callback.h>
#include <ola/rdm/UID.h>
#include <ola/thread/SchedulingExecutorInterface.h>
#include <memory>
#include <map>
#include <string>
#include "plugins/usbpro/WidgetDetectorInterface.h"

namespace ola {
namespace plugin {
namespace usbpro {

using std::auto_ptr;

/*
 * Contains information about the Lumen USB device.
*/
class LumenWidgetInformation {
  public:
    LumenWidgetInformation()
        : uid(0, 0) {
    }
    LumenWidgetInformation(const LumenWidgetInformation &other):
        uid(other.uid) {
    }

    ola::rdm::UID uid;
};


/*
 * Handles widget discovery for Lumen devices.
 */
class LumenWidgetDetector: public WidgetDetectorInterface {
  public:
    typedef ola::Callback2<void,
                           ola::network::ConnectedDescriptor*,
                           const LumenWidgetInformation*> SuccessHandler;
    typedef ola::Callback1<void,
                           ola::network::ConnectedDescriptor*> FailureHandler;

    LumenWidgetDetector(
        ola::thread::SchedulingExecutorInterface *scheduler,
        SuccessHandler *on_success,
        FailureHandler *on_failure,
        unsigned int timeout = 200);
    ~LumenWidgetDetector();

    bool Discover(ola::network::ConnectedDescriptor *descriptor);

  private:
    // Hold the discovery state for a widget
    class DiscoveryState {
      public:
        DiscoveryState():
          discovery_state(INFO_SENT),
          timeout_id(ola::thread::INVALID_TIMEOUT) {
        }
        ~DiscoveryState() {}

        typedef enum {
          INFO_SENT,
          UID_SENT,
        } widget_state;

        LumenWidgetInformation information;
        widget_state discovery_state;
        ola::thread::timeout_id timeout_id;
    };

    ola::thread::SchedulingExecutorInterface *m_scheduler;
    const unsigned int m_timeout_ms;
    auto_ptr<SuccessHandler> m_callback;
    auto_ptr<FailureHandler> m_failure_callback;

    typedef std::map<DispatchingLumenWidget*, DiscoveryState> WidgetStateMap;
    WidgetStateMap m_widgets;

    void HandleMessage(DispatchingLumenWidget *widget,
                       uint8_t label,
                       const uint8_t *data,
                       unsigned int length);
    void HandleUidMessage(DispatchingLumenWidget *widget,
                          const uint8_t *data,
                          unsigned int length);
    void WidgetRemoved(DispatchingLumenWidget *widget);
    void FailWidget(DispatchingLumenWidget *widget);
    void CleanupWidget(DispatchingLumenWidget *widget);
    void DispatchWidget(DispatchingLumenWidget *widget,
                        const LumenWidgetInformation *info);
    void RemoveTimeout(DiscoveryState *discovery_state);
    void SetupTimeout(DispatchingLumenWidget *widget,
                      DiscoveryState *discovery_state);

    static const uint32_t MODEL_MASK = 0xffff0000;
};
}  // usbpro
}  // plugin
}  // ola
#endif  // PLUGINS_USBPRO_LUMENWIDGETDETECTOR_H_
